﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockManagement.Model;
using StockManagement.Commands;
using StockManagement.Helper;
using System.Collections.ObjectModel;
using StockManagement.Models;
using System.Data;

namespace StockManagement.ViewModels
{
    public class MaterialViewModel : BaseViewModel
    {        
        Material _material;

        public MaterialViewModel()
        {
            _material = new Material();
            OnPropertyChanged("MaterialName");
            OnPropertyChanged("MaterialType");
            OnPropertyChanged("MaterialCode");
            OnPropertyChanged("Vendor");
            OnPropertyChanged("MaterialRate");
            OnPropertyChanged("Quantity");
            OnPropertyChanged("Date");
            OnPropertyChanged("MaterialVAT");
        }

        public MaterialViewModel(Material material)
        {
            _material = material;
        }

        public MaterialViewModel(DataRow materialrow)
        {
            _material = new Material(materialrow);
        }

        public Guid MaterialID
        {
            get { return _material.MaterialID; }
        }

        public string MaterialName
        {
            get { return _material.Name; }
            set
            {
                if (ValidateMaterialName(value))
                    _material.Name = value;
                else
                    ShowErrorMessageBox("The Material name already exists", "Duplicate Name");
                OnPropertyChanged("MaterialName");
            }
        }

        public EnumMaterialType MaterialType
        {
            get { return _material.MaterialType; }
            set
            {
                _material.MaterialType = value;
                OnPropertyChanged("MaterialType");
            }
        }

        public string MaterialCode
        {
            get { return _material.MaterialCode; }
            set
            {
                _material.MaterialCode = value;
                OnPropertyChanged("MaterialCode");
            }
        }

        public Guid VendorID
        {
            set { _material.VendorID = value; }
            get { return _material.VendorID; }            
        }

        public string Purchaser
        {
            get { return _material.Purchaser; }
            set
            {
                _material.Purchaser = value;
                OnPropertyChanged("Purchaser");
            }
        }

        public double MaterialRate
        {
            get { return _material.Rate; }
            set
            {
                _material.Rate = value;
                OnPropertyChanged("MaterialRate");                
            }
        }

        public double MaterialVAT
        {
            get { return _material.Vat; }
            set
            {
                _material.Vat = value;
                OnPropertyChanged("MaterialVAT");
            }
        }

        public double TotalPrice
        {
            get { return _material.Rate * (1 + MaterialVAT / 100); }
        }

        public int Quantity
        {
            get { return _material.Quantity; }
            set
            {
                _material.Quantity = value;
                OnPropertyChanged("Quantity");
            }
        }

        public DateTime Date
        {
            get { return _material.Date; }
            set
            {
                _material.Date = value;
                OnPropertyChanged("Date");
            }
        }

        public bool ValidateMaterialName(string materialName)
        {
            return Adapter.ValidateMaterialName(materialName);
        }

        public bool SaveMaterial()
        {
            return Adapter.AddNewMaterial(_material);
        }

        public void DiscardMaterial()
        {
            _material = new Material();
            OnPropertyChanged("MaterialName");
            OnPropertyChanged("MaterialType");
            OnPropertyChanged("MaterialCode");
            OnPropertyChanged("Vendor");
            OnPropertyChanged("MaterialRate");
            OnPropertyChanged("Quantity");
            OnPropertyChanged("Date");
        }

        public bool DeleteMaterial()
        {
            return Adapter.DeleteMaterial(_material.MaterialID);
        }

        public bool UpdateMaterial(UpdateType updattype, DateTime date,Guid vendorID, string code = "", double rate=0,
            int quantity=0, double vat=0,string purchaser="")
        {
            bool vaild = false;
             switch (updattype)
            {
                case UpdateType.Edit:
                    vaild =  Adapter.UpdateEditMaterial(_material,code,vendorID,rate,quantity,date,vat);
                     break;
                case UpdateType.Deliver:
                    vaild = Adapter.UpdateDeliverMaterial(_material,purchaser);
                     break;
            }
            return vaild;
        }
    }

    public class ProductMaterialViewModel :BaseViewModel
    {
        ProductMaterial _prodMaterial;

        public ProductMaterialViewModel(Guid materialID, string materialName, string materialCode)
        {
            _prodMaterial = new ProductMaterial(materialID, materialName, materialCode);
        }

        public ProductMaterialViewModel(ProductMaterial productMaterial)
        {
            _prodMaterial = productMaterial;
        }
    
        public Guid MaterialID 
        {
            get{return _prodMaterial.MaterialID;}
        }

        public string MaterialName
        {
            get { return _prodMaterial.MaterialName; }
        }

        public string MaterialCode
        {
            get { return _prodMaterial.MaterialCode; }
        }
        public int MaterialQuantity
        {
            get { return _prodMaterial.MaterialQuantity; }
            set
            {
                _prodMaterial.MaterialQuantity = value;
                OnPropertyChanged("MaterialQuantity");
            }
        }

        public bool IsSelected
        {
            get { return _prodMaterial.IsSelected; }
            set
            {
                _prodMaterial.IsSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }

        public ProductMaterial Material
        {
            get { return _prodMaterial; }
        }
    }

    public class ProductMaterialListViewModel : ObservableCollection<ProductMaterialViewModel>
    {        
    }
}
